<?php

namespace App\Api\Controllers;

use App\Models\Student;
use App\Models\kly\Kly_camp;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class GetStudentController extends Controller
{
    public function get_students(Request $request)
    {
        $squad_id = $request->get('q');
        $data = Student::where('squad_id',$squad_id)->where('status','=','1')->groupBy('id')->select('id',DB::raw("group_concat(no,'-',en_name) as name"))->get();

        $arr=array();
        $arr[] = array('id'=>0,'text'=>'请选择学生');
        foreach($data as $v){
            $arr[] = array('id'=>$v->id,'text'=>$v->name);
        }
        return $arr;
    }

    public function get_students_by_big_camp(Request $request)
    {
        $q = $request->get('q');
        $students = DB::table('students as stu')->join('squads as s',function($join){
            $join->on('stu.squad_id','=','s.id');
        })->whereIn('stu.squad_id',Kly_camp::where('id',$q)->value('squads'))->where('stu.status','=','1')->orderBy('s.name')->select('stu.id',DB::raw("concat(stu.en_name,'(',s.name,')') as student_name"))->get();
        $arr=array();
        // $arr[] = array('id'=>0,'text'=>'请选择学生');
        foreach($students as $k => $v){
            // $arr[$v->id] = $v->student_name;
            $arr[] = array('id'=>$v->id,'text'=>$v->student_name);
        }
        // dd($arr);
        // $result = array_column($arr, 'text','id');
        // dd($n);
        return $arr;
    }
}
